import { Toast } from 'vant'
import {createRouter,createWebHistory,} from 'vue-router'
const Home = ()=>import('../views/Home.vue')
const Cart = ()=>import('../views/Cart.vue')
const Me = ()=>import('../views/Me.vue')
const Order = ()=>import('../views/Order.vue')
const Store = ()=>import('../views/Store.vue')
const NewOrder =()=>import('../views/CreateOrder.vue')
const Address = ()=>import('../views/Address.vue')
const NewAddRess = ()=>import('../views/NewAddRess.vue')
const Login = ()=>import('../views/Login.vue')
const Regisiter = ()=>import('../views/Regisiter.vue')
const UserInfoEdit =()=>import('../views/UserInfoEdit')
const routes = [
      {
        path: '/',
        redirect: '/home',
      },
      {
        path: '/home',
        name:'home',
        component:Home,
      },
      {
        path: '/cart',
        name:'cart',
        component:Cart,
        meta:{
          isAuth:true
        }
      },
      {
        path: '/me',
        name:'me',
        component:Me,
        meta:{
          isAuth:true
        }
      },
      {
        path: '/order',
        name:'order',
        component:Order,
        meta:{
          isAuth:true
        }
      },
      {
        path:'/store',
        name:'store',
        component:Store
      },
      {
        path:'/neworder',
        name:'neworder',
        component:NewOrder,
        meta:{
          isAuth:true
        }
      },
      {
        path:'/address',
        name:'address',
        component:Address,
        meta:{
          isAuth:true
        }
      },
      {
        path:'/newaddress',
        name:'newaddress',
        component:NewAddRess,
        meta:{
          isAuth:true
        }
      },
      {
        path:'/login',
        name:'login',
        component:Login
      },
      {
        path:'/regisiter',
        name:'regisiter',
        component:Regisiter
      },
      {
        path:'/userinfoedit',
        name:'userinfoedit',
        component:UserInfoEdit
      },

]
const router = createRouter({
  history:createWebHistory(),
  routes
})
router.beforeEach((to,from,next)=>{
   if(to.meta.isAuth){
     if(localStorage.isLogin==='1'){
       next();
     }else{
       Toast.loading('请先登录')
       router.push('/login')
     }
   }else{
     next();
   }
})
export default router